﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using Tsanie.ShellExt.Definitions;
using Tsanie.ShellExt.Properties;

namespace Tsanie.ShellExt
{
    [Guid("CE80D400-D942-4730-92B8-074E9EF12AD8")]
    [ComVisible(true), ExtensionFileTypes("Directory\\Background")]
    public class ManagerContextMenu : ContextMenuHandlerBase
    {
        protected override List<MenuItem> OnQueryMenus()
        {
            List<MenuItem> menus = new List<MenuItem>();

            // 程序系统菜单
            if (this.Files == null || this.Files.Count == 0)
            {
                MenuItem item = new MenuItem
                {
                    Text = "Thumbnail Manager"
                };
                item.MenuItems.Add(new MenuItem
                {
                    Text = "Manage plugins",
                    Image = Resources.setup,
                    OnClick = new EventHandler(manage_Click)
                });
                item.MenuItems.Add(new MenuItem
                {
                    IsSeparator = true
                });
                item.MenuItems.Add(new MenuItem
                {
                    Text = "Load plugins",
                    IsEnable = !Manager.IsPluginLoaded,
                    Image = Resources.on,
                    OnClick = new EventHandler(load_Click)
                });
                item.MenuItems.Add(new MenuItem
                {
                    Text = "Unload plugins",
                    IsEnable = Manager.IsPluginLoaded,
                    OnClick = new EventHandler(unload_Click)
                });

                menus.Add(item);
            }
            else
            {
                if (this.Files.Count <= 0)
                {
#if DEBUG
                    Common.OutputDebugString("OnMenuCreate():: Files.Count == 0.");
#endif
                    return menus;
                }
                // 自定义接口
                ContextMenuBaseProvider provider = Manager.GetCacheProvider<ContextMenuBaseProvider>(this.Files);
                if (provider != null)
                {
                    try
                    {
                        string[] files = new string[this.Files.Count];
                        for (int i = 0; i < this.Files.Count; i++)
                        {
                            files[i] = this.Files[i];
                        }
                        menus.AddRange(provider.GetMenuItems(files));
                    }
                    catch (Exception e)
                    {
                        foreach (string file in this.Files)
                        {
                            Common.OutputDebugString("OnMenuCreate():: '{0}'", file);
                        }
                        Common.OutputError("OnMenuCreate()", e);
                    }
                }
#if DEBUG
                else
                {
                    foreach (string file in this.Files)
                    {
                        Common.OutputDebugString("OnMenuCreate():: Can't find the provider... {0}", file);
                    }
                }
#endif
            }
            return menus;
        }

        #region - Events -

        void load_Click(object sender, EventArgs e)
        {
            Manager.LoadPlugins();
        }
        void unload_Click(object sender, EventArgs e)
        {
            Manager.UnloadPlugins();
        }
        void manage_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(delegate()
            {
                PluginsForm plugins = new PluginsForm();
                System.Windows.Forms.Application.Run(plugins);
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        #endregion
    }
}
